define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'district'], function ($, undefined, Backend, Table, Form, District) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'circle/index' + location.search,
                    add_url: 'circle/add',
                    edit_url: 'circle/edit',
                    del_url: 'circle/del',
                    multi_url: 'circle/multi',
                    //import_url: 'circle/import',
                    table: 'circle',
                }
            });

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='user.nickname']", form).addClass("selectpage")
                    .data("source", "user/user/index")
                    .data("primaryKey", "nickname")
                    .data("field", "nickname")
                    .data("orderBy", "id asc");
                //$("input[name='province_id']", form).addClass("selectpage")
                //    .data("source", "district/index")
                //    .data("primaryKey", "id")
                //    .data("field", "name")
                //    .data("orderBy", "id asc");
                //$("input[name='city_id']", form).addClass("selectpage")
                //    .data("source", "district/index")
                //    .data("primaryKey", "id")
                //    .data("field", "name")
                //    .data("orderBy", "id asc");
                $("input[name='area_id']", form).addClass("selectpage")
                    .data("source", "district/index")
                    .data("primaryKey", "id")
                    .data("field", "name")
                    .data("orderBy", "id asc");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh desc,id desc',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        //{field: 'province_id', title: __('Province_id'), visible: false},
                        //{field: 'province_name', title: __('Province_id'), operate: false},
                        //{field: 'city_id', title: __('City_id'), visible: false},
                        //{field: 'city_name', title: __('City_id'), operate: false},
                        {field: 'area_id', title: __('Area_id'), visible: false},
                        {field: 'area_name', title: __('Area_id'), operate: false},
                        //{field: 'address', title: __('Address'), operate: 'LIKE'},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            $("input[name='province_id']").data('params', function(){
                return {custom:{parent_id:1}};
            });
            var province_id = $("input[name='province_id']").val();
            province_id = 27833;
            $("input[name='city_id']").data('params', function(){
                if(!province_id){
                    province_id = -1;
                }
                return {custom:{parent_id:province_id}};
            });
            var city_id = $("input[name='city_id']").val();
            city_id = 28141;
            $("input[name='area_id']").data('params', function(){
                if(!city_id){
                    city_id = -1;
                }
                return {custom:{parent_id:city_id}};
            });

            $(document).on('change', "input[name='province_id']", function(){
                province_id = $(this).val();
                $("input[name='city_id']").clearSelected();
                $("input[name='area_id']").clearSelected();
            });

            $(document).on('change', "input[name='city_id']", function(){
                city_id = $(this).val();
                $("input[name='area_id']").clearSelected();
            })
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

                District.districtCpcdsEvent();
            }
        }
    };
    return Controller;
});